package com.we823.cms.controller.manage.ex;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.Direction;
import com.we823.cms.common.web.pageable.PageRequest;
import com.we823.cms.common.web.pageable.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.controller.manage.AbstractManageController;
import com.we823.cms.model.extension.Brand;
import com.we823.cms.model.extension.ProductCategory;
import com.we823.cms.model.extension.Product;
import com.we823.cms.model.extension.ProductPhoto;
import com.we823.cms.repository.service.extension.BrandService;
import com.we823.cms.repository.service.extension.ProductCategoryService;
import com.we823.cms.repository.service.extension.ProductPhotoService;
import com.we823.cms.repository.service.extension.ProductService;

@Controller
@RequestMapping(WebConstants.MANAGE_URL+"/product")
public class ProductManageController extends AbstractManageController<Product, Integer> {

	private String baseUrl = WebConstants.MANAGE_URL+"/product/";
	
	private ProductService productService;
	
	private @Resource ProductCategoryService productCategoryService;
	private @Resource BrandService brandService;
	private @Resource ProductPhotoService productPhotoService;
	
	@Autowired
	public ProductManageController(ProductService productService) {
		super(productService);
		super.baseUrl = baseUrl;
		super.entityName="product";
		this.productService=productService;
	}

	@Override
	public PageInfo<Product> getData(PageRequest pageRequest,
									 HttpServletRequest request, Model model, boolean pageable) {
		PageInfo<Product> productPage ;
		int categoryId = RequestUtil.getValueByName(request, "categoryId", -1);
		int brandId = RequestUtil.getValueByName(request, "brandId", -1);
		if(categoryId+brandId==-2) {
			if (pageable) {
				productPage = productService.findAll(pageRequest);
			}else {
				productPage = new PageInfo<>(productService.findAll(getSort(request)));
			}
		}else {
			if(!pageable) {
				pageRequest = null;
			}
			if (categoryId>0 && brandId>0) {
				ProductCategory productCategoryModel = productCategoryService.findOne(categoryId);
				Brand brandModel = brandService.findOne(brandId);
				productPage = productService.findByCategoryAndBrand(productCategoryModel,brandModel,pageRequest);
			}else {
				if(categoryId>0 && brandId==-1) {
					ProductCategory productCategoryModel = productCategoryService.findOne(categoryId);
					productPage = productService.findByCategory(productCategoryModel,pageRequest);
				}else {
					Brand brandModel =  brandService.findOne(brandId);
					productPage = productService.findByBrand(brandModel,pageRequest);
				}
			}
			
		}
		return productPage;
	}
	
	@RequestMapping("photoManage")
	public String photoManage(HttpServletRequest request,Model model) {
		int id = RequestUtil.getValueByName(request, "id", -1);
		boolean hasError = false;
		String message = "";
		if (id>0) {
			Product productModel = productService.findOne(id);
			if (productModel!=null) {
				String description = productModel.getDescription();
				description = description.replace("\r\n", "<br>");
				productModel.setDescription(description);
			}
			model.addAttribute("product",productModel);
		}else {
			hasError = true;
			message = "找不到相关记录";
		}
		model.addAttribute("hasError",hasError);
		model.addAttribute("message",message);
		
		return baseUrl + "photoManage";
	}
	
	@RequestMapping("getPhotos")
	@ResponseBody
	public Model getPhotos(HttpServletRequest request,Model model) {
		int id = RequestUtil.getValueByName(request, "id", -1);
		boolean hasError = false;
		String message = "";
		if (id>0) {
			List<ProductPhoto> photoModels = productPhotoService.findByProductid(id);
			if (photoModels==null || photoModels.isEmpty()) {
				hasError = true;
				message = "暂无图片";
			}else {
				for (ProductPhoto productPhotoModel : photoModels) {
					if (!productPhotoModel.getUrl().startsWith("/u/r")) {
						productPhotoModel.setUrl(WebConstants.PHOTO_PREFIX+productPhotoModel.getUrl());
					}
				}
				model.addAttribute("photos",photoModels);
			}
			
		}else {
			hasError = true;
			message = "ID信息错误";
		}
		model.addAttribute("hasError",hasError);
		model.addAttribute("message",message);
		return model;
	}
	
	@RequestMapping("savePhoto")
	@ResponseBody
	public Model savePhoto(HttpServletRequest request,Model model) {
		int id = RequestUtil.getValueByName(request, "id", -1);
		String url = RequestUtil.getValueByName(request, "url");
		int photoId = RequestUtil.getValueByName(request, "photo_id", -1);
		
		boolean hasError = false;
		String message = "";
		if (id>0) {
			ProductPhoto productPhotoModel = null;
			if(photoId>0) {
				productPhotoModel = productPhotoService.findOne(photoId);
			}else {
				productPhotoModel = new ProductPhoto();
			}
			productPhotoModel.setProductId(id);
			productPhotoModel.setUrl(url);
			try {
				if (photoId>0) {
					productPhotoService.edit(productPhotoModel, photoId);
				}else {
					productPhotoService.edit(productPhotoModel, null);
				}
			   
			}catch(Exception ex) {
				hasError = true;
				message = "新增数据错误";
			}
		}else {
			hasError = true;
			message = "产品ID错误";
		}
		model.addAttribute("hasError",hasError);
		model.addAttribute("message",message);
		return model;
	}
	
	@RequestMapping("editPhoto")
	@ResponseBody
	public Model editPhoto(HttpServletRequest request,Model model) {
		int id = RequestUtil.getValueByName(request, "id", -1);
		boolean hasError = false;
		String message = "";
		if (id>0) {
			ProductPhoto productPhotoModel = productPhotoService.findOne(id);
			if (productPhotoModel!=null) {
				model.addAttribute("photo",productPhotoModel);
			}else {
				hasError = true;
				message = "未能找到图片信息";
			}
		}else {
			hasError = true;
			message = "ID错误，无法找到图片信息";
		}
		model.addAttribute("hasError",hasError);
		model.addAttribute("message",message);
		return model;
	}
	@RequestMapping("delPhoto")
	@ResponseBody
	public Model delPhoto(HttpServletRequest request,Model model) {
		int id = RequestUtil.getValueByName(request, "id", -1);
		boolean hasError = false;
		String message = "";
		if (id>0) {
			productPhotoService.delete(id);
		}else {
			hasError = true;
			message = "ID错误，无法删除";
		}
		model.addAttribute("hasError",hasError);
		model.addAttribute("message",message);
		return model;
	}
	
	@RequestMapping("changeDefaultPhoto")
	@ResponseBody
	public Model changeDefaultPhoto(HttpServletRequest request,Model model) {
		int id = RequestUtil.getValueByName(request, "id", -1);
		boolean hasError = false;
		String message = "";
		if (id>0) {
			ProductPhoto productPhotoModel = productPhotoService.findOne(id);
			if (productPhotoModel!=null) {
				int productId = productPhotoModel.getProductId();
				Product productModel = productService.findOne(productId);
				if (productModel!=null) {
					productModel.setPhoto(productPhotoModel.getUrl());
					productService.edit(productModel, productId);
				}else {
					hasError = true;
					message = "找不到产品信息，无法设置默认图片";
				}
			}else {
				hasError = true;
				message = "无法找到图片信息无法更新信息";
			}
		}else {
			hasError = true;
			message = "ID错误，无法更新信息";
		}
		model.addAttribute("hasError",hasError);
		model.addAttribute("message",message);
		return model;
	}
	
	@Override
	public Sort getSort(HttpServletRequest request) {
		Sort sort = super.getSort(request);
		if (sort==null) {
			sort = new Sort(Direction.DESC,"addtime");
		}
		return sort;
	}


	@Override
	public void setIndetity() {
		setResourceIdentity("ex:product");
	}
}
